package studySpringBoot.com.springboot;

import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import java.util.Map;

public class ZhiGanSpringApplication {

    public static void run(Class clazz){

        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(clazz);
        applicationContext.refresh();

        WebServer webServer =getWebServer(applicationContext);
        webServer.start();

    }

    private static WebServer getWebServer(AnnotationConfigWebApplicationContext applicationContext) {
        //依赖tomcat jetty undertom
        Map<String, WebServer> webServers = applicationContext.getBeansOfType(WebServer.class);
        if (webServers.isEmpty()){
            throw new NullPointerException("没有配置服务依赖");
        }
        if(webServers.size() > 1){
            throw new IllegalStateException("存在多个服务依赖");
        }
        //返回唯一的一个
        return webServers.values().stream().findFirst().get();
    }

}
